﻿using System;
using System.Web.Mvc;
using IterationLife.Extensions.Enums;
using IterationLife.Extensions.MvcApi;
using IterationLife.Mobile.AppServices.Services.Common;
using Util;
using Util.Exceptions;

namespace IterationLife.Mobile.ApiGateway.Areas.Common.Controllers
{
    /// <summary>
    /// 内容资源控制器
    /// </summary>
    public class ContentResourceController : MvcApiController
    {

        /// <summary>
        /// 内容资源服务
        /// </summary>
        private readonly IContentResourceService _newsService;

        /// <summary>
        /// 初始化内容资源控制器
        /// </summary>
        /// <param name="newsService">内容资源服务</param>
        public ContentResourceController(IContentResourceService newsService)
        {
            _newsService = newsService;
        }

        /// <summary>
        /// 热点内容资源列表
        /// </summary>
        /// <param name="size">条数</param>
        /// <param name="codes">分类编码集合</param>
        /// <param name="tags">标签集合</param>
        [HttpGet]
        public ActionResult HotList(int? size = 5, string codes = null, string tags = null)
        {
            if (size < 1)
                size = 5;
            var result = _newsService.HotList(size.SafeValue(), codes);
            return Ok(result, "热点内容资源");
        }

        /// <summary>
        /// 内容资源范围列表
        /// </summary>
        /// <param name="size">条数</param>
        /// <param name="codes">分类编码集合</param>
        /// <param name="tags">标签集合</param>
        /// <param name="id">内容资源编号</param>
        /// <param name="queryType">查询类型</param>
        [HttpGet]
        public ActionResult RangeList(int? size = 5, string codes = null, string tags = null, Guid? id = null, RangeQueryType queryType = RangeQueryType.After)
        {
            if (size < 1)
                size = 5;
            var reuslt = _newsService.RangeList(size.SafeValue(), codes, tags, id, queryType);
            return Ok(reuslt, "内容资源列表");
        }

        /// <summary>
        /// 内容资源详情
        /// </summary>
        /// <param name="id">内容资源编号</param>
        [HttpGet]
        public ActionResult Detail(Guid id)
        {
            if (id.IsEmpty())
                throw new Warning("编号无效");
            var result = _newsService.Detail(id);
            return Ok(result, "内容资源详情");
        }
    }
}